package sgi.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import sgi.details_new.AccountInfoDetails;
import sgi.details_new.ImageDetails;
import sgi.user.User;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

@SuppressWarnings("serial")
public class SGIAccountInfoUI extends SGIClientFrame {

	private SGIAccountInfoController AccountInfoController;
	private JTextField UsernameField;
	private JTextField FNameField;
	private JTextField LNameField;
	private JTextField PhoneField;
	private JTextField EmailField;
	private JTextField BalanceField;
	private JTextField txtDataLayer;
	private JTextField txtDateTime;
	private JTextField txtImagePack;
	private	JTabbedPane tabbedPane;
	private JPanel contentPaneInfo;
	private JPanel contentPanePurchaseHistory;
	private JPanel contentPaneSearchHistory;
	private JTable tablePurchaseHistory;
	private JTable tableSearchHistory;
	/** setting buttons and text fields */
	public SGIAccountInfoUI(User user)  {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 774, 434);
		setResizable(false);
		setTitle("SGI Account Info of "+user.getUsername());
		
		JPanel topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );
		
		// Create the tab pages
		createContentPaneInfo();
		createContentPanePurchaseHistory();
		createContentPaneSearchHistory();

		// Create a tabbed pane
		tabbedPane = new JTabbedPane();
		
		tabbedPane.addTab( "Info", contentPaneInfo );
		tabbedPane.addTab( "Purchase History", contentPanePurchaseHistory );
		tabbedPane.addTab( "Search History", contentPaneSearchHistory );

		topPanel.add( tabbedPane, BorderLayout.CENTER );
		
	}
	
	
	private void createContentPaneInfo()
	{
		
		contentPaneInfo = new JPanel();
		contentPaneInfo.setLayout(null);
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 36, 89, 35);
		contentPaneInfo.add(lblUsername);
		
		UsernameField = new JTextField();
		UsernameField.setBounds(207, 40, 167, 26);
		contentPaneInfo.add(UsernameField);
		UsernameField.setColumns(10);
		UsernameField.setEditable(false);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(10, 101, 89, 35);
		contentPaneInfo.add(lblFirstName);
		
		FNameField = new JTextField();
		FNameField.setBounds(207, 105, 167, 26);
		contentPaneInfo.add(FNameField);
		FNameField.setColumns(10);
		FNameField.setEditable(false);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(10, 162, 89, 35);
		contentPaneInfo.add(lblLastName);
		
		LNameField = new JTextField();
		LNameField.setBounds(207, 166, 167, 26);
		contentPaneInfo.add(LNameField);
		LNameField.setColumns(10);
		LNameField.setEditable(false);
		
		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setBounds(404, 36, 65, 35);
		contentPaneInfo.add(lblPhone);
		
		PhoneField = new JTextField();
		PhoneField.setBounds(594, 40, 141, 26);
		contentPaneInfo.add(PhoneField);
		PhoneField.setColumns(10);
		PhoneField.setEditable(false);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(404, 101, 65, 35);
		contentPaneInfo.add(lblEmail);
		
		EmailField = new JTextField();
		EmailField.setBounds(594, 105, 141, 26);
		contentPaneInfo.add(EmailField);
		EmailField.setColumns(10);
		EmailField.setEditable(false);
		
		JLabel lblBalance = new JLabel("Balance");
		lblBalance.setBounds(404, 162, 65, 35);
		contentPaneInfo.add(lblBalance);
		
		BalanceField = new JTextField();
		BalanceField.setBounds(594, 166, 141, 26);
		contentPaneInfo.add(BalanceField);
		BalanceField.setColumns(10);
		BalanceField.setEditable(false);
			
		JButton btnReturn = new JButton("Return");
		btnReturn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			SGIAccountInfoController.terminate();
			SGIUserAccountMenuController.create(null);
			}
		});
		btnReturn.setBounds(594, 307, 147, 47);
		contentPaneInfo.add(btnReturn);
		
		JLabel lblDatalayersubscription = new JLabel("Data_Layer_Subscription");
		lblDatalayersubscription.setBounds(10, 228, 187, 31);
		contentPaneInfo.add(lblDatalayersubscription);
		
		txtDataLayer = new JTextField();
		txtDataLayer.setBounds(207, 230, 167, 26);
		contentPaneInfo.add(txtDataLayer);
		txtDataLayer.setColumns(10);
		txtDataLayer.setEditable(false);
		
		JLabel lblDatetimesubscription = new JLabel("Date_Time_Subscription");
		lblDatetimesubscription.setBounds(404, 226, 180, 35);
		contentPaneInfo.add(lblDatetimesubscription);
		
		txtDateTime = new JTextField();
		txtDateTime.setBounds(594, 230, 141, 26);
		contentPaneInfo.add(txtDateTime);
		txtDateTime.setColumns(10);
		txtDateTime.setEditable(false);
		
		JLabel lblImagepacksubscription = new JLabel("Image_Pack_Subscription");
		lblImagepacksubscription.setBounds(10, 278, 187, 37);
		contentPaneInfo.add(lblImagepacksubscription);
		
		txtImagePack = new JTextField();
		txtImagePack.setBounds(208, 283, 166, 26);
		contentPaneInfo.add(txtImagePack);
		txtImagePack.setColumns(10);
		txtImagePack.setEditable(false);
	}
	
	private void createContentPanePurchaseHistory()
	{
		contentPanePurchaseHistory = new JPanel();
		contentPanePurchaseHistory.setLayout(null);
		

		Object columnNames[] = {"Description","Time"};
		Object rowData[][] = {};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		          return false;
		        }
		      };
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(contentPanePurchaseHistory.getX()+25, contentPanePurchaseHistory.getY()+20, 700, 300);
		contentPanePurchaseHistory.add(scrollPane);
		tablePurchaseHistory = new JTable(dtm);
		tablePurchaseHistory.setBounds(10, 127, 490, 315);
		//table.setAutoCreateRowSorter(true);


		//table.getRowSorter().toggleSortOrder(2);	//makes trouble with row order

	
		JTableHeader header = tablePurchaseHistory.getTableHeader();
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(tablePurchaseHistory, BorderLayout.CENTER);
		
	}
	/** get controller */
	private void createContentPaneSearchHistory()
	{
		contentPaneSearchHistory = new JPanel();
		contentPaneSearchHistory.setLayout(null);
		
		
		
		Object columnNames[] = {"Description","Time"};
		Object rowData[][] = {};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		          return false;
		        }
		      };
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(contentPaneSearchHistory.getX()+25, contentPaneSearchHistory.getY()+20, 700, 300);
		contentPaneSearchHistory.add(scrollPane);
		tableSearchHistory = new JTable(dtm);
		tableSearchHistory.setBounds(10, 127, 490, 315);




	
		JTableHeader header = tableSearchHistory.getTableHeader();
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(tableSearchHistory, BorderLayout.CENTER);
		
	}
	
	public SGIAccountInfoController getAccountInfoController() {
		return AccountInfoController;
	}
	
	/** setting the controller */
	public void setSGIAccountInfoController(SGIAccountInfoController AccountInfoController) {
		this.AccountInfoController = AccountInfoController;
	}
	
	/** setting all fields in view info */
	public void addAccountInfo(AccountInfoDetails aid)
	{
		UsernameField.setText(aid.getUserName());
		FNameField.setText(aid.getFname());
		LNameField.setText(aid.getLname());
		PhoneField.setText(aid.getPhone());
		EmailField.setText(aid.getEmail());
		BalanceField.setText(String.valueOf(aid.getBalance()));
		txtDataLayer.setText(aid.getDataLayer());
		txtDateTime.setText(aid.getDateTime());
		txtImagePack.setText(aid.getImagePack());
	}
	/** review the UI */
	public void review()
	{
		AccountInfoController.ViewInfo(SGIUserAccountMenuController.getUser());
	}
	
	public void addRowPurchaseHistory (String description,String time)
	{
		DefaultTableModel dtm = (DefaultTableModel) tablePurchaseHistory.getModel();
		dtm.insertRow(tablePurchaseHistory.getRowCount(), new Object[] {description,time});
		tablePurchaseHistory.setModel(dtm);
	}
	
	public void addRowSearchHistory (String description,String time)
	{
		DefaultTableModel dtm = (DefaultTableModel) tableSearchHistory.getModel();
		dtm.insertRow(tableSearchHistory.getRowCount(), new Object[] {description,time});
		tableSearchHistory.setModel(dtm);
	}
	
	public void clearTablePurchaseHistory()
	{
		DefaultTableModel dtm = (DefaultTableModel) tablePurchaseHistory.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}
	
	public void clearTableSearchHistory()
	{
		DefaultTableModel dtm = (DefaultTableModel) tableSearchHistory.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}
}
